Public Class EditProductType

    Private _newProductTypes As New System.Collections.ObjectModel.Collection(Of String)
    Private _deletedProductTypes As New System.Collections.ObjectModel.Collection(Of Integer)

    Private _fileId As Integer

    Public Event ProductTypesChanged()

    Private Sub EditProductType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.ProductTypeTableAdapter.Fill(Me.ProductTypeData.ProductType)

    End Sub

    Public Sub New(ByVal fileId As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _fileId = fileId

        Me.FileProductTypeTableAdapter.Fill(Me.FileProductTypesData.FileProductType, _
                                            fileId)

    End Sub

    Private Sub AddFileProductType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFileProductType.Click

        Dim newProductType As FileProductTypesData.FileProductTypeRow = FileProductTypesData.FileProductType.NewFileProductTypeRow
        newProductType.product_type = ProductTypeList.Text
        newProductType.file_id = _fileId
        Try
            FileProductTypesData.FileProductType.AddFileProductTypeRow(newProductType)
            _newProductTypes.Add(ProductTypeList.Text)
        Catch ex As Exception

        End Try
        FileProductTypeList.Refresh()

    End Sub

    Private Sub RemoveFileProductType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveFileProductType.Click

        Dim entryRow As FileProductTypesData.FileProductTypeRow
        Dim rowsToDelete As New System.Collections.ObjectModel.Collection(Of FileProductTypesData.FileProductTypeRow)

        For Each entry As System.Data.DataRowView In FileProductTypeList.SelectedItems
            entryRow = CType(entry.Row, FileProductTypesData.FileProductTypeRow)
            If entryRow.RowState = DataRowState.Added Then
                _newProductTypes.Remove(entryRow.product_type)
            Else
                _deletedProductTypes.Add(entryRow.fpt_id)
            End If
            rowsToDelete.Add(entryRow)
        Next

        For Each entryRow In rowsToDelete
            FileProductTypesData.FileProductType.RemoveFileProductTypeRow(entryRow)
        Next

        FileProductTypeList.Refresh()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        FileProductTypesDS.CancelEdit()
        Me.Close()

    End Sub

    Private Sub UpdateFileDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateFileDetails.Click

        Dim updated As Integer

        For Each deletedFigure As Integer In _deletedProductTypes
            Me.FileProductTypeTableAdapter.Delete(deletedFigure)
        Next

        FileProductTypesDS.EndEdit()
        updated = Me.FileProductTypeTableAdapter.Update(Me.FileProductTypesData)

        RaiseEvent ProductTypesChanged()

        Me.Close()

    End Sub

End Class